﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;
using System.IO;
using System.Drawing;

namespace ClassLibrary
{
    public class Parameters
    {
        public struct Values
        {
            public static ResourceManager RM;

            #region Local Path

            public static string localPath_File_Log;
            public static string localPath_Dir_Tmp;

            public static string localPath_UserFile
            {
                get;set;
            }
            public static string localPath_UserFileName
            {
                get { return Path.GetFileName(localPath_UserFile); }
            }
            public static string localPath_Root
            {
                get { return Path.GetDirectoryName(localPath_UserFile); }
            }
            public static string localPath_Small
            {
                get { return Path.Combine(localPath_Root, "_Small"); }
            }
            public static string localPath_Medium
            {
                get { return Path.Combine(localPath_Root, "_Medium"); }
            }
            public static string localPath_Project
            {
                get { return Path.Combine(localPath_Root, Path.GetFileNameWithoutExtension(localPath_UserFile)); }
            }
            public static string localPath_GoogleMap
            {
                get { 
                    return Path.Combine(localPath_Project, "MyGoogleMap.html");   
                }
            }
            public static string localPath_GoogleMap_Base
            {
                get { return Path.Combine("Resources", "MyGoogleMap.html"); }
            }
            public static string localPath_DataSet
            {
                get
                {
                    return Path.Combine(localPath_Project, "Dataset.js");
                }
            }
            public static string localPath_DataSet_Base
            {
                get { return Path.Combine("Resources","Dataset_Base.js"); }
            }

            public static string localPath_nConvert
            {
                get { return Path.Combine("./Resources", "nconvert.exe"); }
            }

            public static string localPath_EmptyMap
            { 
                get { return Path.Combine("./Resources","EmptyMap.html"); } 
            }

            public static string localPath_picturesLibrary 
            { 
                get; set; 
            }

            #endregion

            #region Server
            public static string FTPServer;
            public static string MDP;
            public static string Login;
            public static string ServerRootFolder;
            public static string ServerPath_Small
            {
                get
                {
                    return Path.Combine(ServerRootFolder, "_Small");
                }
            }
            public static string ServerPath_Medium
            {
                get
                {
                    return Path.Combine(ServerRootFolder, "_Medium");
                }
            }
            public static string ServerPath_DataSet
            {
                get
                {
                    DirectoryInfo dirInfo = new DirectoryInfo(localPath_Project);
                    string dir = Path.Combine(ServerRootFolder, dirInfo.Name);
                    return Path.Combine(dir, "Dataset.js");
                }
            }
            public static string ServerPath_GoogleMap
            {
                get
                {
                    DirectoryInfo dirInfo = new DirectoryInfo(localPath_Project);
                    string dir = Path.Combine(ServerRootFolder, dirInfo.Name);
                    return Path.Combine(dir, "MyGoogleMap.html");
                }
            }
            public static string ServerRootFolder_Full
            {
                get { return Path.Combine(FTPServer, ServerRootFolder); }
            }
            public static string ServerPath_Small_Full
            {
                get { return Path.Combine(FTPServer, ServerPath_Small); }
            }
            public static string ServerPath_Medium_Full
            {
                get { return Path.Combine(FTPServer, ServerPath_Medium); }
            }
            public static string ServerPath_ProjectFolder
            {
                get
                {
                    return Path.Combine(ServerRootFolder, Path.GetFileNameWithoutExtension(localPath_UserFile));
                }
            }
            public static string ServerPath_ProjectFolder_Full
            {
                get { return Path.Combine(FTPServer, ServerPath_ProjectFolder); }
            }

            #endregion

            public static List<Marker> MarkerList;
            public static List<string> Categories;

            public static decimal Default_MediumWidth
            {
                get{return 150m;}
            }
            public static decimal Default_MediumHeight
            {
                get { return 150m; }
            }
            public static decimal Default_SmallWidth
            {
                get { return 36m; }
            }
            public static decimal Default_SmallHeight
            {
                get { return 36m; }
            }

            //Paris
            public static float default_lat = 48.8566140F;
            public static float default_lng = 2.3522219F;

            //Application
            public static Color Color_linkLabel_AddMarker_Hover = Color.DodgerBlue;
            public static Color Color_linkLabel_AddMarker_Default = Color.SteelBlue;

            //DataSet
            public static string DS_NEW = "m = new Marker";
            public static string DS_BEGIN_CAT = "//BEGIN Categories";
            public static string DS_BEGIN_Marker = "//BEGIN Markeur";
            public static string DS_END = "//END";
            public static string DS_PUSH = "listMarker.push(m);";
            public static string DS_CAT_PUSH = "m.cat.push";

            //O = open, C = close
            public static string DS_DIV_TITLE_O = "<div id='titre' dir='ltr'><b>";
            public static string DS_DIV_TITLE_C = "</b></div>";
            public static string DS_DIV_IMG_O = "<div id='img' dir='ltr'><img src='";
            public static string DS_DIV_IMG_C = "'></div>";
            public static string DS_DIV_EVENT_O = "<div id='description' dir='ltr'>";
            public static string DS_DIV_EVENT_LINK_O = "<a href='";
            public static string DS_DIV_EVENT_LINK_C = "' target='_blank'>";
            public static string DS_DIV_EVENT_C = "</a></div>";

            //Language
            public static string PARAM_LANGUAGE;
        }

        public struct Validation
        {
            public bool success_FTPServer_connection;

            public bool exists_localPath_Dir_Project;
            public bool exists_localPath_File_DataSet;
            public bool exists_localPath_File_GoogleMap;

            public bool exists_serverPath_Dir_Root;
            public bool exists_serverPath_Dir_Small;
            public bool exists_serverPath_Dir_Medium;
            public bool exists_serverPath_Project;
            //public bool exists_serverPath_File_DataSet;
            //public bool exists_serverPath_File_GoogleMap;

            public bool all_Valid
            {
                get
                {
                    if (success_FTPServer_connection &&
                        exists_serverPath_Dir_Root &&
                        exists_serverPath_Dir_Small &&
                        exists_serverPath_Dir_Medium &&
                        exists_serverPath_Project &&
                        //exists_serverPath_File_DataSet &&
                        //exists_serverPath_File_GoogleMap &&
                        exists_localPath_Dir_Project &&
                        exists_localPath_File_DataSet &&
                        exists_localPath_File_GoogleMap)
                        return true;
                    else return false;
                }
            }
        }

        public static void check(out Validation res)
        {
            res = new Validation();
            try
            {
                //FTP
                //-----------------------------------------------------------------
                res.success_FTPServer_connection = FTPUtility.ConnectionTest(true);
                //-----------------------------------------------------------------

                //Local Path
                //-----------------------------------------------------------------
                if (res.success_FTPServer_connection)
                {
                    res.exists_localPath_Dir_Project = Directory.Exists(Values.localPath_Project);
                    res.exists_localPath_File_DataSet = File.Exists(Values.localPath_DataSet);
                    res.exists_localPath_File_GoogleMap = File.Exists(Values.localPath_GoogleMap);
                }
                else
                {
                    res.exists_localPath_Dir_Project = false;
                    res.exists_localPath_File_DataSet = false;
                    res.exists_localPath_File_GoogleMap = false;
                }

                //Server Path
                //-----------------------------------------------------------------
                bool RC = true;
                res.exists_serverPath_Dir_Root = false;
                res.exists_serverPath_Dir_Small = false;
                res.exists_serverPath_Dir_Medium = false;
                //res.exists_serverPath_File_DataSet = false;
                //res.exists_serverPath_File_GoogleMap = false;

                if (res.success_FTPServer_connection)
                {
                    if (!FTPUtility.ExistDirectory(Values.ServerRootFolder_Full, false))
                    {
                        RC = FTPUtility.MakeDirectory(Values.ServerRootFolder);
                        if (!RC) throw new Exception("MakeDirectory failed " + Values.ServerRootFolder);
                        res.exists_serverPath_Dir_Root = FTPUtility.ExistDirectory(Values.ServerRootFolder_Full, false);
                    }
                    else res.exists_serverPath_Dir_Root = true;
                    

                    string RootPath = Path.Combine(Values.FTPServer, Values.ServerRootFolder);

                    RootPath = Path.GetFileName(Values.ServerPath_Small);

                    if (!FTPUtility.ExistDirectory(Values.ServerPath_Small_Full, false))
                    {
                        RC = FTPUtility.MakeDirectory(Values.ServerPath_Small);
                        if (!RC) throw new Exception("MakeDirectory failed " + Values.ServerPath_Small);
                        res.exists_serverPath_Dir_Small = FTPUtility.ExistDirectory(Values.ServerPath_Small_Full, false);
                    }
                    else res.exists_serverPath_Dir_Small = true;


                    if (!FTPUtility.ExistDirectory(Values.ServerPath_Medium_Full, false))
                    {
                        FTPUtility.MakeDirectory(Values.ServerPath_Medium);
                        if (!RC) throw new Exception("MakeDirectory failed " + Values.ServerPath_Medium);
                        res.exists_serverPath_Dir_Medium = FTPUtility.ExistDirectory(Values.ServerPath_Medium_Full, false);
                    }
                    else res.exists_serverPath_Dir_Medium = true;

                    if (!FTPUtility.ExistDirectory(Values.ServerPath_ProjectFolder_Full, false))
                    {
                        FTPUtility.MakeDirectory(Values.ServerPath_ProjectFolder);
                        if (!RC) throw new Exception("MakeDirectory failed " + Values.ServerPath_ProjectFolder);
                        res.exists_serverPath_Project = FTPUtility.ExistDirectory(Values.ServerPath_ProjectFolder_Full, false);
                    }
                    else res.exists_serverPath_Project = true;
                }
            }
            catch (Exception e)
            {
                ErrorManager.WriteLog("Parameters", "Check", e);
            }
        }
    }
}
